Skip to main content

3.1.2 Compressed recoding - inrange

In addition to inlist, there is another logical function that can make it easier to create conditions based on intervals: inrange. If-conditions based on intervals rarely become particularly long, since you usually only need to specify a minimum and a maximum value. But if you operate with several intervals in a single if-expression, inrange can be useful. In any case, the generate- or replace-expression is more compressed compared to normal if-expressions when using this function.

The logical function inrange is set to 1 ("true") if the value in the first argument is higher than or equal to the value in the second argument and lower than or equal to the value in the third argument.

The arguments to the function can be both variables and values ​​(all types).

Example:

  • generate var = 1 if inrange(wealth, 500000, 1000000) (inrange-function = 1 ("true") if wealth is greater than or equal to 500000 and less than or equal to 1000000)

The example above corresponds to this expression:

  • generate var = 1 if wealth >= 500000 & wealth <= 1000000